<!-- 春节不打烊 - 淘宝 -->
<template>
  <div class="page" ref="pageHeight">
    <div class="goods-content">
      <div class="goods-list">
        <product v-for="(item, index) in goodsList" :currencySymbol="userCurrency" :info="item" :key="item.spuNo"
          @taobaoPoint="taobaoPoint" :index="index"></product>
      </div>

      <div class="loading" v-if="goodsList.length == 0 && loading">
        <t-loading></t-loading>
      </div>
      <t-no-data v-if="goodsList.length == 0 && !loading"></t-no-data>
    </div>
    <div class="more" v-if="!noData">
      <Button v-if="goodsList.length > 0" class="btn" :loading="loading" @click="onLoadMore"
        style="border-color: #FF186B;">{{ $t('goods.viewMore')
        }}</Button>
    </div>
    <!-- 禁搜词黑名单 -->
    <tips-text ref="tipsPopRef"></tips-text>
    <!-- 禁搜词灰名单 -->
    <gray-list ref="grayPopRef"></gray-list>
  </div>
</template>

<script>
import Product from "./components/product";
import { activityList } from "@/api/activity";
import TLoading from "@/components/t-loading";
import TSort from "@/components/t-sort";
import TNoData from "@/components/t-no-data";
import { mapState } from "vuex";
import tipsText from "./components/tips-text.vue";
import GrayList from "./components/gray-list.vue";

export default {
  name: "taobao-goods",
  components: { TNoData, TSort, TLoading, Product, tipsText, GrayList },
  props: {
    loadData: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      loading: false, // 加载
      goodsList: [], // 商品列表
      totalPage: 1, // 总页数
      sortPrice: '', // 排序
      noData: false, // 是否还有更多数据
      form: {
				scrollId:'',
        channel: 'TAOBAO',
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 20, // pageSize,示例值(20)
      },
    }
  },
  computed: {
    ...mapState({
      userCurrency: (state) => {
        return state.user.userCurrency.code;
      },
      config: (state) => state.config.config,
      searchAgreement: (state) => {
        // 搜索协议
        let obj = '';
        if (state.config.config) {
          obj = state.config.config.articleMap.CFG_ARTICLE_SEARCH;
        }
        return obj;
      }
    })
  },
  watch: {
    '$route'() {
      this.onSearchGoods();
    },
    loadData() {
      this.onSearchGoods();
    }
  },
  created() {
    this.onSearchGoods();
  },
  methods: {
    // 淘宝埋点
    taobaoPoint(val1, val2) {
      
    },
    onSearchGoods() {
      if (!this.loadData) {
        return false;
      }
      this.goodsList = [];
      this.form = {
				scrollId:'',
        channel: 'TAOBAO',
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 20, // pageSize,示例值(20)
      };
      this.noData = false;
      this.search();
    },
    onDefault() {
      // 重置默认数据
      this.goodsList = [];
			this.form.scrollId = '';
      this.form.pageNo = 1;
      this.noData = false;
    },
    onSearch() {
      // 搜索商品
      this.onDefault();
      this.search();
    },
    search() {
      this.getProductKeywordQuery();
    },
    onLoadMore() {
      // 加载更多
      this.getProductKeywordQuery();
    },
    getProductKeywordQuery() {
      // 搜索文字商品
      if (this.noData == true) {
        return false;
      }
      this.loading = true;
      activityList(this.form).then((res) => {
				this.form.scrollId = res.result.countId;
        // 禁搜词黑名单
        if (res.code == 1010) {
          this.loading = false;
          this.$refs.tipsPopRef.open(res.message);
        } else if (res.code == 200) {
          // 禁搜词灰度名单弹框
          if (res.message) {
            this.$refs.grayPopRef.open(res.message);
          }

          const itemTaobao = res.result.records.reduce((acc, item, index) => {
            return acc + `${item.spuNo},${index};`;
          }, '');

          let data = res.result.records;
          for (let i = 0; i < data.length; i++) {
            this.goodsList.push(data[i]);
          }

          this.form.pageNo = this.form.pageNo + 1;
          if (this.form.pageNo > res.result.pages) {
            this.noData = true;
          }

          this.totalPage = res.result.pages;
          this.loading = false;
        }
      }).catch(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped lang="less">
/deep/.ivu-btn:focus {
  box-shadow: none;
}

/deep/.ivu-input {
  border: none;
  background: #F7F8FB;
  border-radius: 27px;
  padding: 0 31px;
  color: #74767F;
}

.page {
  height: 100%;

  .scroll-content {
    height: 100%;
  }

  .tabs-wrap {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 30px;
    border-bottom: 1px solid @t-border-color;

    .tabs {
      display: flex;
      align-items: center;

      .tab-item {
        display: flex;
        align-items: center;
        padding: 0px 16px;
        opacity: 1;
        color: @t-title-color;
        font-size: 16px;
        height: 46px;
        border-bottom: 2px solid rgba(0, 0, 0, 0);
        margin: 0 60px;
        cursor: pointer;

        &:hover {
          color: @primary-color;
        }
      }

      .tab-active {
        color: @primary-color;
        border-bottom: 2px solid @primary-color;
      }
    }

    .agreement {
      display: flex;
      align-items: center;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      font-size: 14px;

      .agreement-text {
        color: @t-title-color;
      }

      .agreement-value {
        cursor: pointer;
        color: @primary-color;
        font-weight: 500;
      }
    }
  }

  .filter-content {
    margin-bottom: 40px;
    display: flex;
    align-items: center;

    .label {
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      font-size: 24px;
      color: #767B93;
      line-height: 27px;
      margin-right: 22px;
      min-width: 186px;
      cursor: pointer;
    }

    .active-left {
      color: rgb(255, 24, 107);
    }

    .sort {
      margin-right: 12px;
    }
  }

  .goods-content {
    margin-top: 40px;

    .search-value {
      display: flex;
      align-items: center;
      margin-bottom: 12px;
      color: @t-text-color;
      font-size: 14px;

      span {
        color: @primary-color;
      }

      .search-image {
        display: block;
        width: 80px;
      }
    }

    .goods-list {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 22px 12px;
    }
  }

  .more {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin-top: 32px;
    margin-bottom: 32px;

    .btn {
      width: 30%;
    }
  }
}
</style>